<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var countKDifference = function (nums, k) {
      const n = nums.length;
      let count = 0;
      if (n <= 1) return count;
      for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
          if (Math.abs(nums[i] - nums[j]) == k) {
            count++;
          }
        }
      }
      return count;
    };
    console.log(countKDifference([1, 2, 2, 1], 1));
  </script>
</body>

</html>